package com.cypher.encryptionanddecryption.bluetooth;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.cypher.encryptionanddecryption.adapter.BluetoothListAdapter;

import org.apache.commons.lang3.StringUtils;

/**
 * 监听蓝牙状态变化的广播实现类
 * @author: 古霸天
 * @date: 2020/11/5
 */
public class ExpandBroadcastReceiver extends BroadcastReceiver {
    /**
     * 网络是否可以使用
     */
    public static boolean network = false;
    private Activity activity;
    private BluetoothUtilManager bluetoothUtilManager;
    public ExpandBroadcastReceiver(Activity activity, BluetoothUtilManager bluetoothUtilManager) {
        super();
        this.activity = activity;
        this.bluetoothUtilManager = bluetoothUtilManager;
    }
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if(StringUtils.equals(BluetoothAdapter.ACTION_STATE_CHANGED, action)) {
            final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);

            switch(state) {
                case BluetoothAdapter.STATE_OFF:
                    // 暂停停止扫描
                    bluetoothUtilManager.setIsScanning(false);
                    // 蓝牙已打开
                    bluetoothUtilManager.setEnable(false);
                    // 蓝牙已关闭，申请打开蓝牙
                    bluetoothUtilManager.isCheckBlueToothAdapterEnable();
                    break;
                case BluetoothAdapter.STATE_TURNING_OFF:
                    // 蓝牙正在关闭
                    Toast.makeText(activity, "系统运行期间请保持蓝牙打开状态", Toast.LENGTH_LONG).show();
                    break;
                case BluetoothAdapter.STATE_ON:
                    // 蓝牙已打开
                    bluetoothUtilManager.setEnable(true);
                    // 可以开始扫描
                    bluetoothUtilManager.setIsScanning(true);
                    break;
                case BluetoothAdapter.STATE_TURNING_ON:
                    // 蓝牙正在打开中
                    break;
            }
        }

        // 监听网络是否连接
        if (!isNetworkConn()) {
            network = false;
            Toast.makeText(activity, "网络连接不可用，请检查网络", Toast.LENGTH_LONG).show();
        } else {
            network = true;
        }
    }


    /**
     * 判断网络是否连接
     * @return
     */
    private boolean isNetworkConn() {
        ConnectivityManager manager= (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        // 获取WiFi信息
        NetworkInfo wiFiNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (wiFiNetworkInfo != null) {
            // WiFi可用
            if (wiFiNetworkInfo.isAvailable())
                return true;
        }

        // 获取手机网络
        NetworkInfo mobileNetworkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mobileNetworkInfo != null) {
            // 手机网络可用
            if (mobileNetworkInfo.isAvailable())
                return true;
        }
        return false;
    }
}
